package xml;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import cuShapes.Arrow;
import cuShapes.PQShape;

/**
 * Switches between a java ready list of {@link cuShapes.Arrow Arrow} objects, and the {@link xml.ArrowListType ArrowListType} form of the list needed to write to xml.
 * @author mylesc,varunr
 *
 */
public class ArrowCollectionAdapter extends XmlAdapter<ArrowListType, List<PQShape>> {
	
	/**
	 * Marshals a java list of {@link cuShapes.Arrow Arrow} objects into an {@link xml.ArrowListType ArrowListType} object which is capable of being saved in xml.
	 * @param arrowList a list of Arrows to be converted into xml saveable format.
	 * @return ArrowListType an object with all of arrowList's data ready to be saved to xml.
	 */
	public ArrowListType marshal(List<PQShape> arrowList) throws Exception {
		List<Arrow> arrows = new ArrayList<Arrow>();

		for (PQShape e : arrowList) {
			if (e instanceof Arrow) {
				arrows.add((Arrow) e);
			}
		}
		return new ArrowListType(arrows);
	}

	/**
	 * Unmarshals the ArrowListType form of the Arrows and creates a new java list of Arrows.
	 * @param arrowList an object that contains all data saved in an xml file.
	 * @return arrows a list of {@link cuShapes.PQShape PQShapes} filled with data from an xml file.
	 */
	public List<PQShape> unmarshal(ArrowListType arrowList) throws Exception { 
		List<PQShape> arrows = new ArrayList<PQShape>();
        arrows.addAll(arrowList.getArrows());
        
        return arrows;
	}

}
